package com.zkhz.base.security.token;

import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;

import java.lang.reflect.Field;

/**
 * @Description 静态化tokenValue字段
 *
 * @author wuzeqin
 * @date 2020/6/21
 **/
public class TokenValueSupport {

    public static final boolean isSpringSecurity = ClassUtils.isPresent("org.springframework.security.core.Authentication", TokenValueSupport.class.getClassLoader());
    public static boolean tokenValueInited = false;
    public static Field TokenValueField;

    public static Field getTokenValueField(Object details) {
        if (!tokenValueInited) {
            synchronized (TokenValueSupport.class) {
                if (!tokenValueInited) {
                    tokenValueInited = true;
                    Field tokenValueField = ReflectionUtils.findField(details.getClass(), "tokenValue", String.class);
                    if (tokenValueField != null) {
                        ReflectionUtils.makeAccessible(tokenValueField);
                        TokenValueField = tokenValueField;
                    }
                }
            }
        }

        return TokenValueField;
    }
}
